<?
//define('IN_INDEX', TRUE);

session_start();

include('./../config/db_settings.php');
include('pms_inc.php');

$user = $_SESSION[$settings['session_prefix'].'user_id'];
//echo '$user:'.$user;
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">
<head>
<title>Private Message Center</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../templates/<? echo $settings['template'] ?>/style.css" media="all" />
</head>
<script>
function checkUncheckAll(theElement) 
{
   var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length; z++)
	 {
   		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
   		{
	 			theForm[z].checked = theElement.checked;
	 		}
   }
}

function del_confirm(theElement)
{
	 var theForm = theElement.form;
	 var z = 0;
	 var ifChecked = 0;
	 for(z=0; z<theForm.length; z++)
	 {
   		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
   		{
	 			if(theForm[z].checked)
	 			{
	 				ifChecked=1;
	 				break;
	 			}
	 		}
   }
  if(!ifChecked)
  {
  	alert("Please select at least one message that you want to delete.");
  	return false;
  }      
  
var r=confirm("Are you sure you want to delete those selected message(s) permanently?");
if (r==true)
  {
  return true;
  }
else
  {
  return false;
  }
}
</script>    
<body>
<?    
//Are they logged in or not?
if(!$user)
	{
	echo "<br><p>You aren't logged in. Please log in first.</p><br>";
	}

else
	{
	//clean up deleted msgs
	mysql_query("DELETE FROM ".$db_settings['messages_table']." WHERE sender_id='$user' and received>=7");
	
	//Get your private message count
	//Find the percentage that your inbox is full
	$sql = mysql_query ("SELECT count(*) pm_count FROM ".$db_settings['messages_table']." WHERE receiver_id='$user' and received<4");
	$row = mysql_fetch_array ($sql);
	$pm_count = $row['pm_count'];
	
	$percent = $pm_count/'50';
	$percent = $percent * '100';
	?>
	<br>
	<center>
	<b><p><a href="inbox.php">Inbox</a> | <a href="compose.php">Compose</a> | <a href="sent.php">Sentbox</a></b>
	<b><p>Inbox: <? echo "$pm_count"." of 50 Total  |  "."$percent"."% full"; ?></p></b>
	</center>
	<br>
	<?
	//This stuff and the while loop will query, see if you have messages or not, and display them if you do
	$query = "SELECT a.id, b.user_name, a.subject, a.received, a.pm_ts, a.sender_id, a.message FROM ".$db_settings['messages_table']." a join ".$db_settings['userdata_table']." b on a.sender_id=b.user_id WHERE a.receiver_id='$user' and received<4 order by a.id desc";
	$sqlinbox = mysql_query($query);
	
	//Error thingy, ohh no! Dispaly the error and the query so we can diagnose the problem if there is one
	if(!$sqlinbox)
		{
		?>
		<p><? print '$query: '.$query.mysql_error();?></p>
		<?
		}
	
	//There are no rows found for the user that is logged in, so I guess that means they dont have any messages or maybe its all broken  
	elseif (!mysql_num_rows($sqlinbox) )
		{
		?>
		<center><p><b>You have no messages to display</b></p></center>
		<?
		}
	
	//So if there is not an error, and they apparently do have messages we need to get their information and display it.
	else
		{
		//Ok, Lets center this whole table Im going to make just because I like it like that
		//Then we create a table 80% the total width with 4 columns The subject is 75% of the whole table, the sender is 120 pixels (should be plenty) and the select checkboxes only get 25 pixels
		?>
		<p>Note: to read a new message, please click the message subject to read the whole message.</p>
		<center>
		<form name="frm_1" id="frm_1" method="post" action="delete.php">
		<table class="normaltab" border="0" cellpadding="5" cellspacing="1" width="80%">
		<tr>
		  <th width="" valign="top">Subject</th>
		  <th width="120px" valign="top">Sender</th>  
		  <th width="150px" valign="top">Time</th>  
		  <th width="25px" valign="top">Select</th>
		</tr>
		<?
		//Ok cool, now we stick it all into an array and we will dispaly it now
		$i = 1;
		$c = "a";
		while($inbox = mysql_fetch_array($sqlinbox))
			{
			//These are the variables we have the id of the private message, we have the person who sent the message, we have the subject of the message, and yeah thats it
			$pm_id = $inbox['id'];
			$sender = $inbox['user_name'];
			$subject = htmlspecialchars(stripslashes(base64_decode($inbox['subject'])));
			$body = $inbox['message'];
			$received = $inbox['received'];
			$pm_ts = $inbox['pm_ts'];
			$sender_id = $inbox['sender_id'];
			if($i==1){$c = "a";}
			else {$c = "b";}
			$i= $i * -1;
			
			//So lets show the subject and make that a link to the view message page, we will send the message id through the URL to that page so it can be displayed
			//And also let the person wee who sent it to them, if you want you can make that some sort of a link to view more stuff about the user, but Im not doing that here, I did it for my game though, pretty much same as the viewmsg.php page but a different page, and with the senders id
			//And finally the checkboxes that are all stuck into an array and if they are selected we stick the private message id into the array
			//I will only let my users have a maximum of 50 messages, remeber that ok? Because that's the value I will use later for things
			?>
			<tr>
			  <td valign="top" class="<? echo $c; ?>"><span class="small"><? if($received=='0' || $received=='2'){ ?><img src='../templates/<? echo $settings['template'] ?>/images/email.png'><? } ?> <a href="viewmsg.php?msg_id=<? echo $pm_id; ?>"><? echo $subject; ?> <? if(strlen($body)>0){ ?><img src='../templates/<? echo $settings['template'] ?>/images/pages.png'><? } ?></a></span></td>
			  <td valign="top" class="<? echo $c; ?>"><span class="small"><a href = "<? echo "../index.php?mode=user&show_user=$sender_id"; ?>" target="htkou_showuser"><? echo $sender; ?></a></span></td>
			  <td valign="top" class="<? echo $c; ?>"><span class="small"><? echo $pm_ts; ?></span></td>
			  <td valign="top" class="<? echo $c; ?>"><input name="pms[]" type="checkbox" value="<? echo $pm_id; ?>"></td>
			</tr>
			<?
			//This ends the while loop
			}
		?>
		<tr>  
		<td colspan="3" class="<? echo $c; ?>" align='right'><input type="submit" name="Submit" value="Delete Selected" onclick="return del_confirm(this)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select/unselect all</td>
		<td class="<? echo $c; ?>"><input type="checkbox" name="checkall" onclick="checkUncheckAll(this);"/></td>
		</tr>
		</table>
		</form>
		</center>
		<?
		//So this ends the else to see if it is all ok and having messages or not
		}
	
	//This ends that first thing that checks if you are logged in or not
	}
?>

</body>
</html> 